package com.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.api.common.reqres.Request;
import com.api.common.reqres.Response;
import com.api.component.BaseDataComponent;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.NoticeMapper;
import com.api.model.entity.BaseData;
import com.api.model.entity.Notice;
import com.api.model.enumdata.BaseDataCodeEnum;
import com.api.model.enumdata.BaseDataTypeEnum;
import com.api.model.request.DynamicInforDetailRequest;
import com.api.model.request.NullRequest;
import com.api.model.response.DynamicInforDetailResponse;
import com.api.model.response.StudyReportResponse;
import com.api.model.response.ActivityInforResponse;
import com.api.model.response.DynamicInforResponse;
import com.api.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-16
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;
	@Autowired
	private BaseDataComponent baseDataComponent;
	@Autowired
	private NoticeMapper noticeMapper;

	@Override
	public Response<StudyReportResponse> studyReport(Request<NullRequest> request) {
		// TODO Auto-generated method stub
		String userid = request.getHead().getUserid();
		// 获取基础数据
		BaseData baseData = baseDataComponent.getBaseDataByType(BaseDataTypeEnum.notice.getValue(),
				BaseDataCodeEnum.studyReport.getValue());
		// 设置查询条件
		LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<Notice>();
		queryWrapper.eq(Notice::getUserid, Long.valueOf(userid)).eq(Notice::getBigtype, baseData.getCode());
		List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
		if (CollectionUtils.isEmpty(noticeList)) {
			return responseComponent.toError("暂无信息");
		}
		return responseComponent.toSuccess(noticeList);
	}

	@Override
	public Response<ActivityInforResponse> activityInfor(Request<NullRequest> request) {
		// TODO Auto-generated method stub
		String userid = request.getHead().getUserid();
		// 获取基础数据
		BaseData baseData = baseDataComponent.getBaseDataByType(BaseDataTypeEnum.notice.getValue(),
				BaseDataCodeEnum.activityInfor.getValue());
		// 设置查询条件
		LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<Notice>();
		queryWrapper.eq(Notice::getUserid, Long.valueOf(userid)).eq(Notice::getBigtype, baseData.getCode());
		List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
		if (CollectionUtils.isEmpty(noticeList)) {
			return responseComponent.toError("暂无信息");
		}
		return responseComponent.toSuccess(noticeList);
	}

	@Override
	public Response<DynamicInforResponse> dynamicInfor(Request<NullRequest> request) {
		// TODO Auto-generated method stub
		List<DynamicInforResponse> response = noticeMapper.selectDynamicInfor();
		if (CollectionUtils.isEmpty(response)) {
			return responseComponent.toError("暂无信息");
		}
		return responseComponent.toSuccess(response);
	}

	@Override
	public Response<DynamicInforDetailResponse> dynamicInforDetail(Request<DynamicInforDetailRequest> request) {
		// TODO Auto-generated method stub
		DynamicInforDetailRequest dynamicInforDetailRequest = request.getBody();
		requestComponent.checkBody(dynamicInforDetailRequest, DynamicInforDetailRequest.class);
		String userid = request.getHead().getUserid();
		LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<Notice>();
		queryWrapper.eq(Notice::getUserid, Long.valueOf(userid))
				.eq(Notice::getBigtype, dynamicInforDetailRequest.getBigtype())
				.eq(Notice::getSmalltype, dynamicInforDetailRequest.getSmalltype());

		// 更新已读
		LambdaUpdateWrapper<Notice> updateWrapper = new LambdaUpdateWrapper<Notice>();
		updateWrapper.eq(Notice::getBigtype, dynamicInforDetailRequest.getBigtype())
				.eq(Notice::getSmalltype, dynamicInforDetailRequest.getSmalltype()).eq(Notice::getIsread, false);
		Notice notice = new Notice();
		notice.setIsread(true);
		noticeMapper.update(notice, updateWrapper);
		// 返回查询对象
		List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
		return responseComponent.toSuccess(noticeList);
	}
}
